﻿/*
 *  Copyright (c) 2012 Brandon Ellenberger
 *
 *  This file is part of OpSatPlan.
 *
 *  OpSatPlan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpSatPlan is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpSatPlan.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpSatPlan
{
    public class Parameter
    {
        private string mName;
        private PddlType mType;
        private PddlTypeSet mTypeSet;
        private int? mHashCode;

        public Parameter(string name): this(name, null, null)
        {
        }
        public Parameter(string name, PddlType type) : this(name, type, null)
        {
        }
        public Parameter(string name, PddlTypeSet typeSet) : this(name, null, typeSet)
        {
        }
        private Parameter(string name, PddlType type, PddlTypeSet typeSet)
        {
            Assertion.Assert<ArgumentNullException>(name != null, "Parameter name cannot be null.");
            Assertion.Assert<UsageException>(name != string.Empty, "Parameter name cannot be empty string.");
            this.mName = name.ToUpper();
            this.mType = type;
            this.mTypeSet = typeSet;
            this.mHashCode = null;
        }
        public string Name
        {
            get
            {
                return this.mName;
            }
        }
        public PddlType Type
        {
            get
            {
                return this.mType;
            }
        }
        public PddlTypeSet TypeSet
        {
            get
            {
                return this.mTypeSet;
            }
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            else if (obj.GetType() == typeof(Parameter))
                return this == (Parameter)obj;
            else
                return false;
        }

        public static bool operator ==(Parameter x, Parameter y)
        {
            if (System.Object.ReferenceEquals(x, y))
                return true;
            else if ((object)x == null || (object)y == null)
                return false;
            else
                return x.GetHashCode() == y.GetHashCode();
        }

        public static bool operator !=(Parameter x, Parameter y)
        {
            return !(x == y);
        }

        public override int GetHashCode()
        {
            if (this.mHashCode == null)
            {
                int result = 17;
                result = result * 37 + this.mName.ToUpper().GetHashCode();
                if (this.mType != null)
                    result = result * 37 + this.mType.GetHashCode();
                if (this.mTypeSet != null)
                    result = result * 37 + this.mTypeSet.GetHashCode();
                this.mHashCode = result;
            }
            return this.mHashCode.Value;
        }
        public bool TypeMatches(Parameter parm)
        {
            if (this.mType == null && this.mTypeSet == null)
            {
                return true;
            }
            else if (this.mType != null)
            {
                return this.mType.EqualsOrIsParentOf(parm.Type);
            }
            else if (this.mType == null)
            {
                bool result = false;
                int i = 0;

                while (result == false && i < this.mTypeSet.Count)
                {
                    if (this.mTypeSet[i] != null)
                    {
                        result = this.mTypeSet[i].EqualsOrIsParentOf(parm.Type);
                    }
                    i++;
                }
                return result;
            }
            else
            {
                return PddlType.BaseObjectType.EqualsOrIsParentOf(parm.Type);
            }
        }

        public bool TypeMatches(PddlObject obj)
        {
            if (this.mType == null && this.mTypeSet == null)
            {
                return true;
            }
            else if (this.mType != null)
            {
                return this.mType.EqualsOrIsParentOf(obj.Type);
            }
            else if (this.mType == null)
            {
                bool result = false;
                int i = 0;

                while (result == false && i < this.mTypeSet.Count)
                {
                    if (this.mTypeSet[i] != null)
                    {
                        result = this.mTypeSet[i].EqualsOrIsParentOf(obj.Type);
                    }
                    i++;
                }
                return result;
            }
            else
            {
                return PddlType.BaseObjectType.EqualsOrIsParentOf(obj.Type);
            }
        }
    }
}
